package com.arvin.skin.core;

import android.app.Activity;
import android.content.Context;
import android.os.CpuUsageInfo;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;

import com.arvin.skin.core.utils.SkinResources;
import com.arvin.skin.core.utils.SkinThemeUtils;

import java.lang.reflect.Constructor;
import java.util.HashMap;
import java.util.Observable;
import java.util.Observer;

public class SkinLayoutFactory implements LayoutInflater.Factory2 ,Observer{
    private static final String TAG = "SkinLayoutFactory";
    SkinAttribute skinAttribute;
    private Activity activity;

    public SkinLayoutFactory(Activity activity) {
        this.activity = activity;
        this.skinAttribute = new SkinAttribute(SkinThemeUtils.getSkinTypeface(activity));
    }

    private static final String[] mClassPrefixList = {
            "android.widget.",
            "android.view.",
            "android.webkit"
    };



    private static final HashMap<String, Constructor<? extends View>> sConstructorMap =
            new HashMap<String, Constructor<? extends View>>();

    static final Class<?>[] mConstructorSignature = new Class[]{
            Context.class, AttributeSet.class};


    @Override
    public View onCreateView(String name, Context context, AttributeSet attrs) {
        return this.onCreateView(null, name, context, attrs);
    }

    @Override
    public View onCreateView(View parent, String name, Context context, AttributeSet attrs) {
        View view = createViewFromTag(name, context, attrs);
        skinAttribute.load(view,attrs);
        Log.e(TAG,"onCreateView create by SkinLayoutFactory");
        return view;
    }

    private View createViewFromTag(String name, Context context, AttributeSet attrs) {
        if (name.contains(".")) {
            return createView(name, context, attrs);
        } else {
            View view = null;
            for (String prefix : mClassPrefixList) {
                view = createView(prefix + name, context, attrs);
                if (view != null) {
                    break;
                }
            }
            return view;
        }
    }

    private View createView(String name, Context context, AttributeSet attrs) {
        try {
            Constructor<? extends View> constructor = sConstructorMap.get(name);
            if (constructor == null) {
                Class<? extends View> aClass = context.getClassLoader().loadClass(name).asSubclass(View.class);
                constructor = aClass.getConstructor(mConstructorSignature);
                sConstructorMap.put(name, constructor);
            }
            return constructor.newInstance(context,attrs);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
     }


    @Override
    public void update(Observable o, Object arg) {
        skinAttribute.setTypeface(SkinThemeUtils.getSkinTypeface(activity));
        skinAttribute.applySkin();
        SkinThemeUtils.updateStatusBar(activity);

    }
}
